//
//  MainView.swift
//  xgwjinterconnection
//  主页面
//  Created by WangWeiShuo on 2025/8/13.
//

import SwiftUI

// MARK: - 1. 定义 Tab 枚举
enum Tab: CaseIterable {
    case home, screen, control, mine
    
    var title: String {
        switch self {
        case .home:     return NSLocalizedString("main_nav_home", comment: "首页")
        case .screen:  return NSLocalizedString("main_nav_screen", comment: "投屏")
        case .control:  return NSLocalizedString("main_nav_control", comment: "控制")
        case .mine:  return NSLocalizedString("main_nav_mine", comment: "我的")
        }
    }
    
    var icon: String {
        switch self {
        case .home:     return "bg_home_normal"
        case .screen:  return "bg_screen_normal"
        case .control:  return "bg_control_normal"
        case .mine: return "bg_mine_normal"
        }
    }
    
    var iconPressed: String {
        switch self {
        case .home:     return "bg_home_pressed"
        case .screen:  return "bg_screen_pressed"
        case .control:  return "bg_control_pressed"
        case .mine: return "bg_mine_pressed"
        }
    }
}


struct MainView: View {
    // 记录当前选中的选项卡索引
//        @State private var selectedTab = 0
    @State private var selectedTab: Tab = .home
        
    var body: some View {
//        NavigationStack{
            // 垂直布局：内容区 + 自定义 tab bar
            VStack(spacing: 0) {
                // 2.1 内容区
                ZStack {
                    switch selectedTab {
                    case .home:
                        HomeView()
                    case .screen:
                        ScreenView()
                    case .control:
                        ControlView()
                    case .mine:
                        MineView()
                    }
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .animation(.easeInOut(duration: 0.2), value: selectedTab)
                
                // 2.2 自定义 tab bar
                CustomTabBar(selected: $selectedTab)
                    .padding(.horizontal, 16)   // 左右间距
                    .padding(.bottom, 20)        // 适配无 Home 键机型
            }
            .edgesIgnoringSafeArea(.bottom)     // 让 tab bar 延伸到安全区底部
            .navigationBarBackButtonHidden()
//        }
    }
}

// MARK: - 3. 自定义 Tab Bar
struct CustomTabBar: View {
    @Binding var selected: Tab
    
    var body: some View {
        HStack(spacing: 0) {
            ForEach(Tab.allCases, id: \.self) { tab in
                Button {
                    selected = tab
                } label: {
                    VStack(spacing: 4) {
                        Image(selected == tab ? tab.iconPressed : tab.icon).resizable().frame(maxWidth: 30,maxHeight: 30)

                        Text(tab.title)
                            .font(.caption2)
                    }
                    .frame(maxWidth: .infinity)
                    .foregroundColor(selected == tab ? .white : Color("color_gray_cccccc"))
                    .padding(.vertical, 10)
                }
            }
        }
        .background(
            Capsule()                           // 圆角胶囊
                .fill(Color.blue)               // 背景颜色
                .shadow(color: .black.opacity(0.1),
                        radius: 8, x: 0, y: 2)
        )
        .frame(height: 70)                      // 固定高度
    }
}

#Preview{
    MainView()
}
